/**
 * 
 */
var WebSocketController = {};

WebSocketController = function() {
};

WebSocketController.prototype.connect = function(socketDest) {
	//Declare a stompclient which will connect to the server
	console.log("Connecting..");
	this.socket = new SockJS(socketDest);
	
    console.log("SockJS init..");
    var stompClient = Stomp.over(this.socket);
    
    console.log("stompClient init..");

	// Now that a stomp client is defined, its time to open a connection
    // 1) First we connect to the websocket server
    // Notice that we dont pass in username and password as Spring Security
    // has already provided the server with the Principal object containing user credentials
    // 2) The last argument is a callback function which is called when connection succeeds
    stompClient.connect('', '', function(frame) {
        // In production code remove the next line
        console.log("Connected: " + frame);

        // Now subscribe to a topic of interest.
        // Refer to Java class WebsocketBroadcastController.java#processMessageFromClient(SimpleMessage message)
        // WebsocketBroadcastController is waiting for connections. Upon successful connection, it subscribes to
        // the "/topic/simplemessagesresponse" destination where the server will echo the messages.
        // When a broadcast message is received by the client on that destination, it will be shown by appending
        // a paragraph to the DOM in the client browser.
        stompClient.subscribe("/topic/alarmmessagesresponse", function(servermessage) {//Callback when server responds
            var msg = JSON.parse(servermessage.body);

            EventBus.dispatch(EventType.ALARM_RECEIVED, this, msg);
            /*
            alert("sev:" + msg.sev + ", alarmId:" + msg.alarmId + ", loc:" + msg.loc + 
            		", alarmMsg:" + msg.alarmMsg + ", eventTime:" + msg.eventTime);
             */
        });
    });        
};